<?php
// +----------------------------------------------------------------------
// | BB [ BB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024~2034 https://www.bobahappy.com.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: luowei Team <luowei@bobahappy.com>
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\platform;

use app\Request;
use app\services\platform\${tableClass}Services;
use think\facade\Db;

/**
 * ${tableComment}controller
 * Class ${tableClass}Controller
 * @package app\api\controller\platform
 */
class ${tableClass}Controller
{
    /**
     * services
     * @var ${tableClass}Services
     */
    protected $services;

    public function __construct(${tableClass}Services $services)
    {
        $this->services = $services;
    }
	
	/**
	 * 获取列表
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function lst(Request $request)
	{
	    $where = $request->getMore([
	        ['page', 1],
	        ['limit', 20],
	        ['keywords', '']
	    ]);
	    return app('json')->success($this->services->getList($where['keywords'],$where['page'],$where['limit']));
	}
	
	/**
	 * 保存
	 * @return mixed
	 * @throws \Psr\SimpleCache\InvalidArgumentException
	 */
	public function save(Request $request)
	{
	    $data = $request->postMore([
    	<#list tableProperties as value>
    		<#if value.dataType == 'INT'>
			['${value.columnName}', 0],
			<#elseif value.dataType == 'VARCHAR'>
			['${value.columnName}', ''],
			<#elseif value.dataType == 'TIMESTAMP'>
			['${value.columnName}', ''],
			<#elseif value.dataType == 'DATE'>
			['${value.columnName}', ''],
			<#elseif value.dataType == 'DATETIME'>
			['${value.columnName}', ''],
			<#elseif value.dataType == 'DICIMAL'>
			['${value.columnName}', 0],
			</#if>
		</#list>
	    ]);
	<#list tableProperties as value>
		<#if value.isNull == false && value.columnName !='id' && value.columnName !='status'>
		if ($data['${value.columnName}'] == '') {
	        return app('json')->fail('${value.comment}');
	    }
		</#if>
	</#list>
	    
	    $result = $this->services->save($data);
		if($result){
			return app('json')->success('保存成功');
		}else{
			return app('json')->fail('保存失败');
		}
	}
	
	<#if formWay == 0>
	/**
	 * 创建新增表单
	 * @return mixed
	 * @throws \FormBuilder\Exception\FormBuilderException
	 */
	public function create(Request $request)
	{
	    $data = $request->getMore([
            ['id', 0],
        ]);
        return app('json')->success($this->services->add((int)$data['id']));
	}
	</#if>
	
	/**
	 * 删除
	 * @param $id
	 * @return mixed
	 * @throws \Psr\SimpleCache\InvalidArgumentException
	 */
	public function del($id)
	{
        if (!$id) return app('json')->fail(100100);
        $this->services->del($id);
        return app('json')->success(100002);
	}
	
	<#list methods as value>
	/**
	 * ${value.methodName}
	 * @param array $where
	 * @return array
	 */
	public function ${value.methodName}()
	{
	    return $this->search([])->field('${value.properties}')->where('is_del',0)->order('id desc')->select()->toArray();
	}	
	</#list>
}



